package com.pig4cloud.pigx.system.websocket.app.subscribe;

import com.pig4cloud.pigx.common.customize.redis.constants.CommonRedisConstants;
import com.pig4cloud.pigx.system.constant.RedisHashKeyConstant;
import com.pig4cloud.pigx.system.entity.SystemSymbol;
import com.pig4cloud.pigx.system.websocket.app.message.SendMessage;
import com.pig4cloud.pigx.system.websocket.app.vo.AbstractTopicVO;
import com.pig4cloud.pigx.system.websocket.app.vo.SymbolQuotationsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 币对行情
 */
@Slf4j
@Component
public class SymbolQuotations extends RequestDataAbstractTopic {

	@Autowired
	private RedisTemplate redis;

	public static final String API_NAME = "symbol_quotations";

	public SymbolQuotations() {
		init();
	}

	private void init() {
		wsPush();
	}

	private void wsPush() {
		ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
		service.scheduleAtFixedRate(() -> {
			if (subList.entrySet().size() > 0) {
				try {
					// 获取所有币对
					Set<String> keys = redis.opsForHash().keys(RedisHashKeyConstant.SYMBOL_LIST);
					List<SymbolQuotationsVO> list = new ArrayList<>();
					keys.forEach(e -> {
						Object lastPrice = redis.opsForValue().get(CommonRedisConstants.OKX + CommonRedisConstants.LAST_PRICE + e);
						Object openPrice = redis.opsForValue().get(CommonRedisConstants.OKX + CommonRedisConstants.OPEN_PRICE + e);
						SymbolQuotationsVO vo = new SymbolQuotationsVO();
						vo.setName(e);
						SystemSymbol symbol = (SystemSymbol) redis.opsForHash().get(RedisHashKeyConstant.SYMBOL_LIST, e);
						if (symbol != null)  {
							vo.setImage(symbol.getImage());
							vo.setChain(symbol.getChain());
						}
						if (lastPrice != null) {
							vo.setLastPrice(new BigDecimal(lastPrice.toString()));
							// 计算涨跌幅
							if (openPrice != null) {
								BigDecimal decimal = new BigDecimal(openPrice.toString());
								BigDecimal multiply = (vo.getLastPrice().subtract(decimal))
										.divide(decimal, 8, RoundingMode.DOWN)
										.multiply(new BigDecimal("100")).setScale(2,RoundingMode.DOWN);
 								vo.setPriceFloat(multiply);
							}
							list.add(vo);
						}
					});
					// 币对行情没有入参 所有用户返回统一数据
					for (Map.Entry<WebSocketSession, AbstractTopicVO> entry : subList.entrySet()) {
						try {
							entry.getKey().sendMessage(SendMessage.getTextMessage(list));
						} catch (Exception e) {
							unSub(entry.getKey());
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					log.error("webSocket---symbol_quotations---币对行情Error");
				}
			}
		},0, 1000, TimeUnit.MILLISECONDS);
	}

}
